<template>
  <el-card style="margin: 10px 0px">
    <div class="header">
      <!-- 选项卡:左侧 -->
      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visite"></el-tab-pane>
      </el-tabs>
      <!-- 右侧 -->
      <div class="right">
        <span @click="setDate">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <el-date-picker type="daterange" range-separator="-" start-placeholder="入住时间" end-placeholder="离开时间"
          size="small" value-format="YYYY-MM-DD" v-model="timeArr" />
      </div>
    </div>
    <div class="body">
      <el-row>
        <el-col :span="18" :xs="24">
          <!-- 左侧柱状图 -->
          <div class="container">
            <v-chart autoresize :option="getOptions()"></v-chart>
          </div>
        </el-col>
        <el-col :span="6">
          <h1>门店{{ activeName == "sale" ? "销售额" : "访问量" }}排名</h1>
          <!-- 销售额与访问量排行版的结构 -->
          <ul v-if="activeName == 'sale'">
            <li v-for="(item, index) in data.orderRank" :key="index">
              <span :class="{ active: index < 3 }">{{ item.no }}</span>
              <span>{{ item.name }}</span>
              <span>{{ item.count }}</span>
            </li>
          </ul>
          <ul v-else>
            <li v-for="(item, index) in data.userRank" :key="index">
              <span :class="{ active: index < 3 }">{{ item.no }}</span>
              <span>{{ item.name }}</span>
              <span>{{ item.count }}</span>
            </li>
          </ul>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { ref } from "vue";
//引入时间插件dayjs
import dayjs from "dayjs";
//收集选项卡勾选的数据
const activeName = ref("sale");
//收集日历的数据
// 日历:value-format 可以格式化数据 YYYY MM DD
const timeArr: any = ref([]);
//点击今日span事件回调
const setDate = () => {
  //dayjs使用与moment一模一样的
  const time = dayjs().format("YYYY-MM-DD");
  console.log(time);
  timeArr.value = [time, time];
};
//点击本周span事件回调
const setWeek = () => {
  const start = dayjs().day(1).format("YYYY-MM-DD");
  const end = dayjs().day(7).format("YYYY-MM-DD");
  timeArr.value = [start, end];
};
//本月
const setMonth = () => {
  const start = dayjs().startOf("month").format("YYYY-MM-DD");
  const end = dayjs().endOf("month").format("YYYY-MM-DD");
  timeArr.value = [start, end];
};
//本年
const setYear = () => {
  const start = dayjs().startOf("year").format("YYYY-MM-DD");
  const end = dayjs().endOf("year").format("YYYY-MM-DD");
  timeArr.value = [start, end];
};

//柱状图的配置项
const getOptions = () => {
  return {
    //标题组件title
    title: {
      text: activeName.value == "sale" ? "销售额" : "访问量",
      left: "50%",
      //文字样式
      textStyle: {
        color: "yellowgreen",
      },
    },
    //xy轴
    xAxis: {
      //均匀分布
      type: "category",
      data: props.data.orderFullYearAxis,
    },
    yAxis: {
      //轴线出现
      axisLine: {
        show: true,
      },
      //y轴的刻度
      axisTick: {
        show: true,
      },
    },
    //布局
    grid: {
      left: 30,
      bottom: 20,
    },
    series: {
      type: "bar",
      data:
        activeName.value == "sale"
          ? props.data.orderFullYear
          : props.data.userFullYear,
      //文本标签
      label: {
        show: true,
      },
      //柱条的样式
      itemStyle: {
        color: {
          type: "linear",
          x: 0,
          y: 0,
          x2: 0,
          y2: 1,
          colorStops: [
            {
              offset: 0,
              color: "red", // 0% 处的颜色
            },
            {
              offset: 1,
              color: "blue", // 100% 处的颜色
            },
          ],
          global: false, // 缺省为 false
        },
      },
    },
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow",
      },
    },
  };
};
//props接受父组件传递过来的来自于服务器数据
const props = defineProps(["data"]);


</script>

<style scoped lang="scss">
::v-deep(.el-tabs__item) {
  color: yellowgreen;
}

::v-deep(.el-tabs__active-bar) {
  background: yellowgreen !important;
}

.header {
  position: relative;

  .right {
    position: absolute;
    right: 0;
    top: 0;

    span {
      margin-right: 10px;
      font-size: 14px;
      color: yellowgreen;
    }
  }
}
</style>

<style scoped>
.container {
  height: 300px;
}

ul {
  list-style: none;
}

ul li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 10px 0px;
}

span.active {
  display: inline-block;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
  line-height: 18px;
}
</style>
